{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 03线性回归的简洁实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:52.522009Z",
     "iopub.status.busy": "2023-08-18T07:01:52.521295Z",
     "iopub.status.idle": "2023-08-18T07:01:54.610713Z",
     "shell.execute_reply": "2023-08-18T07:01:54.609677Z"
    },
    "origin_pos": 2,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import torch\n",
    "from torch.utils import data\n",
    "from d2l import torch as d2l"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.616404Z",
     "iopub.status.busy": "2023-08-18T07:01:54.615685Z",
     "iopub.status.idle": "2023-08-18T07:01:54.643472Z",
     "shell.execute_reply": "2023-08-18T07:01:54.642512Z"
    },
    "origin_pos": 5,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "true_w = torch.tensor([2, -3.4])\n",
    "true_b = 4.2\n",
    "features, labels = d2l.synthetic_data(true_w, true_b, 1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.648232Z",
     "iopub.status.busy": "2023-08-18T07:01:54.647744Z",
     "iopub.status.idle": "2023-08-18T07:01:54.653335Z",
     "shell.execute_reply": "2023-08-18T07:01:54.652317Z"
    },
    "origin_pos": 8,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "def load_array(data_arrays, batch_size, is_train=True):  #@save\n",
    "    \"\"\"构造一个PyTorch数据迭代器\"\"\"\n",
    "    dataset = data.TensorDataset(*data_arrays)\n",
    "    return data.DataLoader(dataset, batch_size, shuffle=is_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.657592Z",
     "iopub.status.busy": "2023-08-18T07:01:54.656999Z",
     "iopub.status.idle": "2023-08-18T07:01:54.661787Z",
     "shell.execute_reply": "2023-08-18T07:01:54.660785Z"
    },
    "origin_pos": 11,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "batch_size = 10\n",
    "data_iter = load_array((features, labels), batch_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.665574Z",
     "iopub.status.busy": "2023-08-18T07:01:54.664999Z",
     "iopub.status.idle": "2023-08-18T07:01:54.673523Z",
     "shell.execute_reply": "2023-08-18T07:01:54.672688Z"
    },
    "origin_pos": 13,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[tensor([[-1.3116, -0.3062],\n",
       "         [-1.5653,  0.4830],\n",
       "         [-0.8893, -0.9466],\n",
       "         [-1.2417,  1.6891],\n",
       "         [-0.7148,  0.1376],\n",
       "         [-0.2162, -0.6122],\n",
       "         [ 2.4048, -0.3211],\n",
       "         [-0.1516,  0.4997],\n",
       "         [ 1.5298, -0.2291],\n",
       "         [ 1.3895,  1.2602]]),\n",
       " tensor([[ 2.6073],\n",
       "         [-0.5787],\n",
       "         [ 5.6339],\n",
       "         [-4.0211],\n",
       "         [ 2.3117],\n",
       "         [ 5.8492],\n",
       "         [10.0926],\n",
       "         [ 2.1932],\n",
       "         [ 8.0441],\n",
       "         [ 2.6943]])]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "next(iter(data_iter))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.677177Z",
     "iopub.status.busy": "2023-08-18T07:01:54.676580Z",
     "iopub.status.idle": "2023-08-18T07:01:54.680914Z",
     "shell.execute_reply": "2023-08-18T07:01:54.680130Z"
    },
    "origin_pos": 20,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "# nn是神经网络的缩写\n",
    "from torch import nn\n",
    "\n",
    "net = nn.Sequential(nn.Linear(2, 1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.684561Z",
     "iopub.status.busy": "2023-08-18T07:01:54.684036Z",
     "iopub.status.idle": "2023-08-18T07:01:54.690673Z",
     "shell.execute_reply": "2023-08-18T07:01:54.689754Z"
    },
    "origin_pos": 29,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "net[0].weight.data.normal_(0, 0.01)\n",
    "net[0].bias.data.fill_(0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "origin_pos": 33,
    "tab": [
     "pytorch"
    ]
   },
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.695575Z",
     "iopub.status.busy": "2023-08-18T07:01:54.694922Z",
     "iopub.status.idle": "2023-08-18T07:01:54.699373Z",
     "shell.execute_reply": "2023-08-18T07:01:54.698348Z"
    },
    "origin_pos": 41,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "loss = nn.MSELoss()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.703905Z",
     "iopub.status.busy": "2023-08-18T07:01:54.703368Z",
     "iopub.status.idle": "2023-08-18T07:01:54.708081Z",
     "shell.execute_reply": "2023-08-18T07:01:54.706987Z"
    },
    "origin_pos": 50,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [],
   "source": [
    "trainer = torch.optim.SGD(net.parameters(), lr=0.03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.712705Z",
     "iopub.status.busy": "2023-08-18T07:01:54.712113Z",
     "iopub.status.idle": "2023-08-18T07:01:54.922720Z",
     "shell.execute_reply": "2023-08-18T07:01:54.921580Z"
    },
    "origin_pos": 55,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "epoch 1, loss 0.000248\n",
      "epoch 2, loss 0.000103\n",
      "epoch 3, loss 0.000103\n"
     ]
    }
   ],
   "source": [
    "num_epochs = 3\n",
    "for epoch in range(num_epochs):\n",
    "    for X, y in data_iter:\n",
    "        l = loss(net(X) ,y)\n",
    "        trainer.zero_grad()\n",
    "        l.backward()\n",
    "        trainer.step()\n",
    "    l = loss(net(features), labels)\n",
    "    print(f'epoch {epoch + 1}, loss {l:f}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2023-08-18T07:01:54.927464Z",
     "iopub.status.busy": "2023-08-18T07:01:54.927072Z",
     "iopub.status.idle": "2023-08-18T07:01:54.935672Z",
     "shell.execute_reply": "2023-08-18T07:01:54.934585Z"
    },
    "origin_pos": 60,
    "tab": [
     "pytorch"
    ]
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "w的估计误差： tensor([-0.0010, -0.0003])\n",
      "b的估计误差： tensor([-0.0003])\n"
     ]
    }
   ],
   "source": [
    "w = net[0].weight.data\n",
    "print('w的估计误差：', true_w - w.reshape(true_w.shape))\n",
    "b = net[0].bias.data\n",
    "print('b的估计误差：', true_b - b)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  },
  "required_libs": []
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
